package com.sayice.gomoku.game;

import com.sayice.gomoku.pojo.Player;
import com.sayice.gomoku.pojo.Room;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 游戏
 */
public abstract class Game {
    /**
     * 在线人数
     */
    public static int onlineCount;
    
    /**
     * 最大房间号
     */
    public static int maxRoomId;
    
    /**
     * 玩家 map
     */
    public static final Map<String, Player> playerMap = new ConcurrentHashMap<>();
    
    /**
     * 空闲玩家列表
     */
    public static final List<String> freeList = new CopyOnWriteArrayList<>();
    
    /**
     * 房间 map
     */
    public static final Map<Integer, Room> roomMap = new ConcurrentHashMap<>();
    
    /**
     * 创建对局
     *
     * @param player1 玩家1
     * @param player2 玩家2
     */
    public static void createGame(Player player1, Player player2) throws IOException {
        System.out.println("createGame(): start");
        // 能够开始游戏的条件为双方都不在对局中
        // 不满足条件时无响应
        if (player1.inGame() || player2.inGame()) {
            return;
        }
        int roomId = ++Game.maxRoomId;
        Room room = new Room(roomId);
        room.addPlayers(player1, player2);
        System.out.println("createGame(): finished");
    }
}
